import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_crop/image_crop.dart';
import 'package:karyo/karyo.dart';
import 'package:karyo/src/login/bloc/login_bloc.dart';

class CropImageRoute extends StatefulWidget {
  CropImageRoute(this.image);

  File image; //原始图片路径

  @override
  _CropImageRouteState createState() => new _CropImageRouteState();
}

class _CropImageRouteState extends State<CropImageRoute> {
  double baseLeft; //图片左上角的x坐标
  double baseTop; //图片左上角的y坐标
  double imageWidth; //图片宽度，缩放后会变化
  double imageScale = 1; //图片缩放比例
  Image imageView;
  final cropKey = GlobalKey<CropState>();

  @override
  Widget build(BuildContext context) {
    LoginBloc loginBloc = BlocProvider.of(context);

    return Scaffold(
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        appBar: AppBar(
          textTheme: TextTheme(
              headline6: TextStyle(
                  color: COLOR_PRIMARY,
                  fontWeight: FontWeight.bold,
                  fontSize: 16)),
          leading: buildBackButton(context, COLOR_PRIMARY),
          iconTheme: IconThemeData(color: COLOR_PRIMARY),
          backgroundColor: Theme.of(context).scaffoldBackgroundColor,
          titleSpacing: 0,
          title: buildAppBarTitle("裁剪图片", COLOR_PRIMARY, context),
          // actions: [
          //   IconButton(
          //       icon: Icon(FlutterIcons.vertical_align_bottom_mdi),
          //       onPressed: () {})
          // ],
        ),
        body: Container(
          height: 600,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Container(
                height: 300,
                child: Crop.file(
                  widget.image,
                  key: cropKey,
                  aspectRatio: 1.0,
                  alwaysShowGrid: true,
                  
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                SizedBox(
                  height: 150,
                ),
                Center(
                  child: FlatButton(
                    color: COLOR_PRIMARY,
                    onPressed: () {
                      _crop(widget.image, loginBloc);
                    },
                    child: Text('ok'),
                  ),
                )
              ])
            ],
          ),
        ));
  }

  Future<void> _crop(File originalFile, LoginBloc loginBloc) async {
    final crop = cropKey.currentState;
    final area = crop.area;
    if (area == null) {
      //裁剪结果为空
      print('裁剪不成功');
    }
    await ImageCrop.requestPermissions().then((value) {
      if (value) {
        ImageCrop.cropImage(
          file: originalFile,
          area: crop.area,
        ).then((value) {
          // upload(value);
          print('----- $value');
          loginBloc.setSelectedAvatarFile(value);
          Navigator.of(context).pop();
        }).catchError((e) {
          print('裁剪不成功: $e');
        });
      } else {
        loginBloc.setSelectedAvatarFile(originalFile);
      }
    });
  }
}
